#include"iostream"

using namespace std;

const int N = 100010;

int n, m;
string op;
int h[N], ph[N], hp[N], tsize;

void heap_swap(int t, int u) {
    swap(ph[hp[t]], ph[hp[u]]);
    swap(hp[t], hp[u]);
    swap(h[t], h[u]);
}

void down(int u) {
    int t = u;
    if (u * 2 <= tsize && h[u * 2] < h[t]) t = u * 2;
    if (u * 2 + 1 <= tsize && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
    if (u != t) {
        heap_swap(t, u);
        down(t);
    }
}

void up(int u) {
    while (u / 2 && h[u / 2] > h[u]) {
        heap_swap(u, u / 2);
        u /= 2;
    }
}

int main()
{
	scanf("%d", &n);

    int k, x;
    while (n--) {
        cin >> op;
        if (op == "I") {
            cin >> x;
            h[++tsize] = x; ph[++m] = tsize; hp[tsize] = m; up(tsize);
        } else if (op == "PM") {
            cout << h[1] << endl;
        } else if (op == "DM") {
            heap_swap(1, tsize--);
            down(1);
        } else if (op == "D") {
            cin >> k;
            k = ph[k];
            heap_swap(k, tsize--);
            down(k);
            up(k);
        } else {
            cin >> k >> x;
            k = ph[k];
            h[k] = x;
            down(k);
            up(k);
        }
    }
    return 0;
}